const app= getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('-----coupon---options----')
    console.log(options)
    console.log('----coupon----options----')
    let that=this;
    var parent_id=options.parent_id;

    if(parent_id!=undefined){
        this.setData({
            parent_id:parent_id
          })
          wx.setStorageSync('parent_id',parent_id);
    }else{
        parent_id = wx.getStorageSync('parent_id');
    }

    wx.showShareMenu({
        menus: ['shareAppMessage', 'shareTimeline'],
        success(res) {
          console.log(res)
        },
        fail(e) {
          console.log(e)
        }
      })       
      var pages = getCurrentPages();
  console.log(pages);   

    var deviceInfo = wx.getDeviceInfo();   
  let d = app.globalData;
    that.setData({
        width:d.windowWidth,
        height:d.windowHeight,
        statusBarHeight:d.statusBarHeight,
        navHeight:d.navHeight,
        marginTop:d.navTitleTop,
        bottom:d.bottom,
        domain:d.domain,
        imgUrl:d.imgUrl,
        platform:deviceInfo.platform
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
      this.getIndex();
        let that = this;
        var  tab = wx.getStorageSync('tabBar');
    if(tab == '' || tab == undefined){
        app.getMenu();
        setTimeout(function(){ 
        //console.log("延时1秒后执行");
        that.getTabBar().init('storeWait');
    }, 1000);
    }else{
        this.getTabBar().init('store');
    } 
       // this.getIndex(); 
       setTimeout(function(){ 
        //console.log("延时1秒后执行");
        that.getLocation(); 
      }, 1000);

  },
  getIndex:function(parent_id){
    let that = this;    
    console.log('coupon----getIndex')  
    var store_id = wx.getStorageSync('store_id');
    var open = wx.getStorageSync('open');
    //console.log(this.data.lat);
    wx.request({
      url: app.globalData.domain+'/wechat/company/getIndex',
      data:{from:1},
      success:res=>{
          console.log(res)
          var data=res.data;
          var model=[];
          var models=data.models;
          console.log(data);
  
        that.setData({
         domain:app.globalData.domain,
         shareTitle:data.shareTitle,
         shareLogo:data.shareLogo,
         shareIndex:data.shareIndex,
         list:data.list,
         company_img:data.company_img
     

        })
      }
  })

},

getLocation(){
    console.log('getlocation')
   let that = this;
   wx.authorize({
    scope: 'scope.userFuzzyLocation',
    success(res) {
        console.log(res)
        if(res.errMsg == 'authorize:ok'){
            console.log('authorize')
            wx.getFuzzyLocation({
                success: function(res) {
                    console.log('ok');
                    console.log(res)
                    let lat = res.latitude;
                    let lng= res.longitude
                   // var now_lat = wx.getStorageSync('now_lat');
                   // var now_lng = wx.getStorageSync('now_lng');
                    that.setData({
                        lat:lat,
                        lng:lng
                    })
                },
                error(error){
                    console.log('error');
                }
            })
        }else{
            console.log('authorize_error')  
        }
    },
    fail(err) {
        console.log('err')   
        console.log(err)   
    }                    
})
},
gotoNavigate:function(e){
   console.log(e);
       
   var latitude=e.currentTarget.dataset.lat;
   var longitude=e.currentTarget.dataset.lng;       
  var position=e.currentTarget.dataset.name;
  var address=e.currentTarget.dataset.address;
 
  wx.navigateTo({
    url: '../map/navigate?latitude='+latitude+'&longitude='+longitude+'&position='+position+'&address='+address,
    success:res=>{
        console.log(res);
    }
  })
  
 },
 getDistance:function(latitude1,longitude1,latitude2,longitude2){
     //console.log(ee);
   let that=this;
   console.log('latitude1:'+latitude1+' longitude1:'+longitude1+' latitude2:'+latitude2+' longitude2:'+longitude2);
   wx.request({
     url: app.globalData.domain+'/wechat/home/getDistance',
     data:{
       from:latitude1+','+longitude1,
       to:latitude2+','+longitude2
     },
     success:res=>{
        // console.log(res);
       let distance =  res.data.distance;
        that.setData({
            distance:distance
        })
       // wx.setStorageSync('distance', distance)
     }
   })
 },
callPhone(e){
    console.log(e);
    var number=e.currentTarget.dataset.number;
    wx.makePhoneCall({
      phoneNumber: number,
      success: (res) => {},
      fail: (res) => {},
      complete: (res) => {},
    })
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})